package mindshare;

import info.androidhive.slidingmenu.Notificador;
import info.androidhive.slidingmenu.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import data_retrieval.ManageFriendship;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

public class FriendsListAdapter extends ArrayAdapter<FriendRequest> {
	Context mc;
	private int type;
	private String name;
	private List<FriendRequest> list;
	MenuItem m;
	SessionManager session;
	List<NameValuePair> params = new ArrayList<NameValuePair>();

	public FriendsListAdapter(Context c, int type, List<FriendRequest> list) {
		super(c, type, list);
		mc = c;
		this.list=list;
		this.type=type;
	}
	
	public FriendsListAdapter(Context c, int type, List<FriendRequest> list, MenuItem m) {
		super(c, type, list);
		this.mc = c;
		this.m=m;
		this.list=list;
		this.type=type;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View itemView = convertView;
		if (itemView == null) {
			itemView = LayoutInflater.from(mc).inflate(type, parent, false);
		}
		TextView tvUserName = (TextView) itemView.findViewById(R.id.tvUserName);
		tvUserName.setText(list.get(position).getUserName());

		TextView tvTime = (TextView) itemView.findViewById(R.id.tvTime);
		tvTime.setText(list.get(position).getTime());

		TextView tvMail = (TextView) itemView.findViewById(R.id.tvEmail);
		tvMail.setText(list.get(position).getEmail());
		session= new SessionManager(mc);
		HashMap<String, String> user = session.getUserDetails();
		name = user.get(SessionManager.KEY_NAME);
		if(type==R.layout.friendtable_add){
			Button btnAdd = (Button) itemView.findViewById(R.id.btnAdd);
			btnAdd.setOnClickListener(new Button.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					params.clear();
					params.add(new BasicNameValuePair("nickname", name));
					params.add(new BasicNameValuePair("nickname_2", list.get(position).getNickname()));
					new ManageFriendship(mc, params, "state", m, "add").execute();
					Notificador n = new Notificador(mc);
					n.responder(list.get(position).getNickname(), name+ " send you a friend request");
				}
			});
		} else if(type == R.layout.friendtable_add_delete){
			Button btnDelete = (Button) itemView.findViewById(R.id.btnDelete);
			btnDelete.setOnClickListener(new Button.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					params.clear();
					params.add(new BasicNameValuePair("nickname_2", name));
					params.add(new BasicNameValuePair("nickname", list.get(position).getNickname()));
					new ManageFriendship(mc, params, "state", m, "deleteRequest").execute();
					
				}
			});
			
			Button btnAdd = (Button) itemView.findViewById(R.id.btnAdd);
			btnAdd.setOnClickListener(new Button.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Log.d("ACCEPT", list.get(position).getNickname());
					params.clear();
					params.add(new BasicNameValuePair("nickname_2", name));
					params.add(new BasicNameValuePair("nickname", list.get(position).getNickname()));
					new ManageFriendship(mc, params, "state", m, "accept").execute();
					Notificador n = new Notificador(mc);
					n.responder(list.get(position).getNickname(), name+ " accepted your friend request.");
				}
			});
		} else if(type == R.layout.friendtable_delete){
			Button btnDelete = (Button) itemView.findViewById(R.id.btnDelete);
			btnDelete.setOnClickListener(new Button.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					params.clear();
					params.add(new BasicNameValuePair("nickname", name));
					params.add(new BasicNameValuePair("nickname_2", list.get(position).getNickname()));
					new ManageFriendship(mc, params, "state", m, "delete").execute();
				}
			});
		}
		return itemView;
	}
}
